package com.zizo.autohome.carinfo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zizo.util.ConnectionPool;
import com.zizo.util.Util;

public class UpdateBasicParam {
	private final Connection conn = ConnectionPool.getConnection("autohome_other", "localhost");

	public final void update() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bp_name,bp_mid from autohome_other.other_core_basicparam";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String bp_name = rs.getString(1);
				int cid = rs.getInt(2);
				try {
					bp_name = Util.getMatched(bp_name, "(.*)([12][09][0198][0-9])款?(.*)").group(2);
					updateData(Integer.parseInt(bp_name), cid);
				} catch (Exception e) {
					System.out.println(bp_name);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public final void updateData(int year, int cid) {
		PreparedStatement ps = null;
		String sql = "UPDATE autohome_other.other_core_basicparam set bp_maxyear=? where bp_mid=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, year);
			ps.setInt(2, cid);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionPool.close(ps);
		}
	}

	public void updatecarname() {
		String sql = "update other_core_basicparam set bp_cname=(select autohome_name from carvoice_core.core_brand_autohome where autohome_autohomeId=bp_cid)";
		Statement st = null;
		try {
			st = conn.createStatement();
			st.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		UpdateBasicParam up = new UpdateBasicParam();
		up.update();
		up.updatecarname();
	}
}
